package com.ruoyi.publish.service.impl;

import com.ruoyi.publish.mapper.BrowseMapper;
import com.ruoyi.publish.service.BrowseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class BrowseServiceImpl implements BrowseService {
@Autowired
 private BrowseMapper browseMapper;
    @Override
    public int insertRandom(int count) {
         return browseMapper.insertRandom(count);
    }

    @Override
    public List<Map<String, Object>> getTopColumn() {
        return browseMapper.getTopColumns();
    }

    @Override
    public List<Map<String, Object>> getTop10Articles() {
        return browseMapper.getTop10Articles();
    }

    /**
     * LinkedHashMap<>()与HashMap<>()的区别
     * HashMap：不保证元素的顺序，元素的迭代顺序可能与插入顺序不同，也可能随时间变化（例如扩容后）
     * LinkedHashMap：维护元素的插入顺序（或访问顺序），迭代时按插入顺序返回元素。
     * @return
     */
    @Override
    public Map<String, Object> getTraffic() {
        Map<String, Object> trafficStats = browseMapper.dayWeekMonthYear();
        Map<String, Object> trafficResult = new LinkedHashMap<>();
        trafficResult.put("daily", Map.of("total_visits", trafficStats.get("day")));
        trafficResult.put("weekly", Map.of("total_visits", trafficStats.get("week")));
        trafficResult.put("monthly", Map.of("total_visits", trafficStats.get("month")));
        trafficResult.put("yearly", Map.of("total_visits", trafficStats.get("year")));
        return trafficResult;
    }

    @Override
    public List<Map<String, Object>> getTrafficByDay() {
        return browseMapper.getTrafficByDay();
    }
}
